package inventory.master.controller;

import implementor.CategoryMasterImplementor;
import inventory.master.view.CategoryManagementView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.sql.SQLIntegrityConstraintViolationException;

import javax.swing.JOptionPane;

import dao.CategoryMasterDao;

import model.CategoryMaster;

import utility.Constants;
import utility.Validater;

public class CategoryManagementControler implements ActionListener
{
	private CategoryManagementView view;
	private CategoryMaster model;
	private CategoryMaster[]CategoryMasters;
	private static int index=0;
	
	
/*****************************************CONSTRUCTORS*************************************************************/
	public CategoryManagementControler() 
	{
	
	}

	public CategoryManagementControler(CategoryManagementView view, CategoryMaster model) 
	{
		super();
		this.view = view;
		this.model = model;
		view.actionListener(this);
		CategoryMasterDao cd=new CategoryMasterImplementor();
		try {
			CategoryMasters=cd.retreiveAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

/**********************************************STATIC PART*************************************************************/
	private void reactOnSave()
	{
		String catName=view.gettCatName().getText();
		
		model=new CategoryMaster(catName, catName,1);
		CategoryMasterDao cd=new CategoryMasterImplementor();
		try
		{
			cd.add(model);
			CategoryMasters=cd.retreiveAll();  // refresh the CategoryMaster array
			index=0;
			JOptionPane.showMessageDialog(null, "Data successfully added");
		}
	
		catch(SQLIntegrityConstraintViolationException e)
		{
			JOptionPane.showMessageDialog(null, "Name already exist, please use another one");
			e.printStackTrace();
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully added,Please try again !");
			e.printStackTrace();
		
		}
		
		
	}
	
	private void toggleStatus() throws Exception
	{
		String catName=view.gettCatName().getText();
		
		CategoryMasterDao campdao=new CategoryMasterImplementor();
		CategoryMaster c=null;
		try
		{
			c=campdao.retreive("catName",catName);
			if(view.getLblActive().getText().equals("ACTIVE"))
			       c.setStatus(Constants.INACTIVE);
			if(view.getLblActive().getText().equals("INACTIVE"))
				c.setStatus(Constants.ACTIVE);
		}
		catch(Exception e)
		{
			
			e.printStackTrace();
		}
		
		
		campdao.delete(c);
	}
	
	private void reactOnUpdate() throws Exception
	{
		String catname=view.gettCatName().getText();
		
		CategoryMasterDao cd=new CategoryMasterImplementor();
		model=cd.retreive("catName", catname);
		model.setCatName(catname);
		
		try
		{
			cd.update(model);
			JOptionPane.showMessageDialog(null, "Data successfully updated");
		}
	
		catch(SQLIntegrityConstraintViolationException e)
		{
			JOptionPane.showMessageDialog(null, "Name already exist, please use another one");
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully added,Please try again !");
		
		}
		// The database is updated now , get all the sellers again
		 this.CategoryMasters=cd.retreiveAll();
	}
	
	private void reactOnSearch() throws Exception
	{
		String catname=view.gettCatName().getText();
		CategoryMasterDao cd=new CategoryMasterImplementor();
		
		try
		{
		 model=cd.retreive( "catName", catname);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		fillViewFormFromModel();
		// search has been done we need to update the index
		for(int i =0;i<CategoryMasters.length;i++)
			{
			  if(CategoryMasters[i].equals(catname))
			  {
				  index=i;
			  }
			}
			 
		
		
	}
	

	private void reactOnBack() throws Exception
	{
		if(index>-1)
		{
		model=this.CategoryMasters[index];
		fillViewFormFromModel();
		index=index-1;
		}
		else
			index=0;
	}
	
   private void reactOnForward() throws Exception
	{
		
	
		if(index<CategoryMasters.length && index>-1)
		{
			model=this.CategoryMasters[index];
			fillViewFormFromModel();
			index=index+1;
		}
			
	}
	
	private void fillViewFormFromModel()
	{
		view.gettCatName().setText(model.getCatName());
		view.getTcatCode().setText(model.getCatCode());
		if(model.getStatus() == 1)
		  {
			view.getLblActive().setText("ACTIVE");
			view.getBtnStatus().setText("DEACTIVATE");
		  }
		if(model.getStatus() == 0)
		  {
			view.getLblActive().setText("INACTIVE");
			view.getBtnStatus().setText("ACTIVATE");
		  }
		
	}
	
	
/**********************************************DYNAMIC PART*************************************************************/	
	

	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		String k=arg0.getActionCommand();
		Validater validates = new Validater(view);
		String message = validates.validateForEmpty();
		if(k.contains("SAVE"))
		{
			
			if(message.equals(""))
			{
			   reactOnSave();
			   validates.refresh();
			}
			else
				JOptionPane.showMessageDialog(null,message);
		}
		if(k.contains("MARK INACTIVE"))
		{
			try {
				if(message.equals(""))
				{
					toggleStatus();
					validates.refresh();
				}
				else
					JOptionPane.showMessageDialog(null, message);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(k.contains("UPDATE"))
		{
			try {
				if(message.equals(""))
				{
					reactOnUpdate();
					validates.refresh();
				}
				else
					JOptionPane.showMessageDialog(null, message);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(k.contains("<<"))
		{
			try {
				System.out.println("Back");
				reactOnBack();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(k.contains("SEARCH"))
		{
			try {
				if(message.equals(""))
				{
					reactOnSearch();
					validates.refresh();
				}
				else
					JOptionPane.showMessageDialog(null, message);
				
			
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(k.contains(">>"))
		{
			try {
				reactOnForward();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(k.contains("CLEAR"))
		{
			
		}
		if(k.contains("EXIT "))
		{
			
		}
		
	}
}
